/*
 * @title: 权限组件
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-03-25 21:38:33
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-04-18 23:45:32
 * @version: 1.0.6
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
 */
import store from '@/store';

const HtRouterView = (view) => import(/* webpackChunkName: "app.asyimp" */'@/components/HtRouterView');

export const asyncLoadedView = (view) => {
  if (process.env.NODE_ENV === 'production')
    return () => import(/* webpackChunkName: "app.asyimp" */ `@/views/${view}`);
  else
    return (resolve) => require([`@/views/${view}`], resolve);
};
/**
 * @todo: 通用校验权限方法，判断按钮级权限
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-03-27 15:11:08
 * @param {*} value
 */
export function checkPermission(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles;
    const permissionRoles = value;

    const hasPermission = roles.some((role) => {
      return permissionRoles.includes(role);
    });
    return hasPermission;
  } else {
    console.error(`need roles! Like v-permission="['admin','editor']"`);
    return false;
  }
}

/**
 * @todo: 根据菜单权限数据，生成路由组件对象
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-03-27 09:45:44
 * @param {*} routers 后台buildMenus返回的菜单数据
 * @param {*} lastRouter
 * @param {*} type 是否生成子路由
 */
export const buildRouters = (routers) => {
  // console.log(routers);
  const router = [];
  routers.forEach((el) => { // todo: 一级
    const rt = {};
    // rt['path'] = '/' + el.lr.p;
    rt['path'] = el.lr.p;
    rt['component'] = HtRouterView;
    rt['name'] = el.lr.cm;
    const meta = {
      title: el.lr.t,
      code: el.lr.c,
      noCache: el.lr.ca
    };
    if (el.lr.o)
      meta['icon'] = el.lr.o;
    rt['meta'] = meta;
    rt['redirect'] = 'noredirect';
    rt['alwaysShow'] = true;
    const secChild = [];
    const secChilds = el.lc;
    if (secChilds)
      secChilds.forEach((el2) => { // todo: 二级
        const rt2 = {};
        rt2['path'] = el2.p;
        rt2['name'] = el2.cm;
        const meta2 = {
          title: el2.t,
          code: el2.c
        };
        if (el2.o)
          meta2['icon'] = el2.o;
        rt2['meta'] = meta2;
        // fixme: 支持二级菜单为叶子菜单模式 2022-04-07
        if (+el2.ty !== 2) {
          rt2['redirect'] = 'noredirect';
          rt2['alwaysShow'] = true;
          rt2['component'] = HtRouterView;
        } else
        if (el2.o) {
          meta2['picon'] = el2.o;
          if (el2.cp)
            rt2['component'] = asyncLoadedView(el2.cp);
          else rt2['url'] = el2.u;
        }
        const thirdChild = [];
        const thirdChilds = el2.lc;
        if (thirdChilds) {
          thirdChilds.forEach((el3) => { // todo: 三级
            const rt3 = {};
            rt3['path'] = el3.p;
            if (el3.cp)
              rt3['component'] = asyncLoadedView(el3.cp);
            else
              rt3['url'] = el3.u;
            rt3['name'] = el3.cm;
            const meta3 = {
              title: el3.t,
              code: el3.c
            };
            if (el2.o)
              meta3['picon'] = el2.o;
            if (el3.o)
              meta3['icon'] = el3.o;
            rt3['meta'] = meta3;
            thirdChild.push(rt3);
          });
          rt2['children'] = thirdChild;
        }
        secChild.push(rt2);
      });
    rt['children'] = secChild;
    router.push(rt);
  });
  return router;
};
